package com.yogee.topseller.activity.subscription;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import com.trello.rxlifecycle.LifecycleTransformer;
import com.yogee.core.base.HttpView;
import com.yogee.core.http.listener.HttpOnNextListener;
import com.yogee.core.http.subscribers.BaseSubscriber;
import com.yogee.core.utils.ToastUtils;
import com.yogee.topseller.R;
import com.yogee.topseller.activity.CommonWebView;
import com.yogee.topseller.activity.SubscriptionActivity;
import com.yogee.topseller.activity.my.MyOrderActivity;
import com.yogee.topseller.bean.ResultBean;
import com.yogee.topseller.http.HttpManager;
import com.yogee.topseller.utils.AppUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ContractDistributionActivity extends AppCompatActivity implements HttpView {

    @BindView(R.id.rb_common)
    RadioButton rbCommon;
    @BindView(R.id.rb_search)
    RadioButton rbSearch;
    @BindView(R.id.rb_invite)
    RadioButton rbInvite;
    @BindView(R.id.tv_tip)
    TextView tvTip;
    @BindView(R.id.tv_tip_2)
    TextView tvTip2;
    @BindView(R.id.ll_tip_2)
    LinearLayout llTip2;
    @BindView(R.id.title_white_left_back)
    LinearLayout titleWhiteLeftBack;
    @BindView(R.id.title_white_title)
    TextView titleWhiteTitle;
    @BindView(R.id.title_white_right_txt)
    TextView titleWhiteRightTxt;

    private String type;//0认购 ，1预约
    private String proId;
    private String delivery = "0";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contract_distribution);
        ButterKnife.bind(this);
        if (getIntent() != null) {
            type = getIntent().getStringExtra("type");
            proId = getIntent().getStringExtra("proId");
        }
        init();
    }

    private void init() {
        titleWhiteTitle.setText("合同配送");
        titleWhiteRightTxt.setText("完成");
        titleWhiteRightTxt.setVisibility(View.VISIBLE);
    }

    @OnClick({R.id.title_white_left_back, R.id.rb_common, R.id.rb_search, R.id.rb_invite, R.id.ll_tip_2, R.id.title_white_right_txt})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rb_common:
                rbCommon.setTextColor(getResources().getColor(R.color.white));
                rbSearch.setTextColor(Color.parseColor("#4a4a4a"));
                rbInvite.setTextColor(Color.parseColor("#4a4a4a"));
                tvTip.setText("根据快递公司提供为准");
                llTip2.setVisibility(View.GONE);
                delivery = "0";
                break;
            case R.id.rb_search:
                rbSearch.setTextColor(getResources().getColor(R.color.white));
                rbCommon.setTextColor(Color.parseColor("#4a4a4a"));
                rbInvite.setTextColor(Color.parseColor("#4a4a4a"));
                tvTip.setText("由搜搜出单安排第一时间送到您手中");
                llTip2.setVisibility(View.VISIBLE);
                tvTip2.setText("费用商议");
                delivery = "1";
                break;
            case R.id.rb_invite:
                rbInvite.setTextColor(getResources().getColor(R.color.white));
                rbCommon.setTextColor(Color.parseColor("#4a4a4a"));
                rbSearch.setTextColor(Color.parseColor("#4a4a4a"));
                tvTip.setText("我们将为您安排最近的办事处");
                llTip2.setVisibility(View.VISIBLE);
                tvTip2.setText("地址查询");
                delivery = "2";
                break;
            case R.id.ll_tip_2:
                if ("费用商议".equals(tvTip2.getText())) {
                    startActivity(new Intent(ContractDistributionActivity.this, CommonWebView.class).putExtra("title", "费用商议"));
                } else if ("地址查询".equals(tvTip2.getText())) {
                    startActivity(new Intent(ContractDistributionActivity.this, CommonWebView.class).putExtra("title", "地址查询"));
                }
                break;
            case R.id.title_white_left_back:
                finish();
                break;
            case R.id.title_white_right_txt:
                if (!AppUtil.isFastDoubleClick()) {
                    delivery(proId, delivery);
                }
                break;
        }
    }

    private void delivery(String proId, String delivery) {
        HttpManager.getInstance().delivery(proId, delivery).subscribe(new BaseSubscriber(new HttpOnNextListener<ResultBean>() {
            @Override
            public void onNext(ResultBean o) {
                if (o.getResult().equals("1")) {
                    if (type.equals("1")) {//预约
                        startActivity(new Intent(ContractDistributionActivity.this, MyOrderActivity.class));
                        finish();
                    } else if (type.equals("0")) {//认购
                        startActivity(new Intent(ContractDistributionActivity.this, SubscriptionActivity.class));
                        finish();
                    } else {
                        finish();
                    }
                }
            }
        }, this));
    }

    @Override
    public void onLoading() {

    }

    @Override
    public void loadingFinished() {

    }

    @Override
    public void showMsg(String msg) {
        ToastUtils.showToast(this, msg);
    }

    @Override
    public <T> LifecycleTransformer<T> bindRecycler() {
        return null;
    }
}
